//
//  GameSurface.m
//  JoyPad
//
//  Created by Leone Keijzer on 07/09/2011.
//  Copyright (c) 2011 Cameraland. All rights reserved.
//

#import "GameSurface.h"

@implementation GameSurface

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        shipLocation.x = 0;
        shipLocation.y = 0;
        shipDirection = 0;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    //Draw spaceship
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextTranslateCTM(context,  ( self.bounds.size.width / 2 ) + shipLocation.x, 
                                    ( self.bounds.size.height / 2 ) + shipLocation.y );
    CGContextSaveGState(context);
    //Draw spaceship form
    CGContextMoveToPoint(context, 0.0, -15.0);
    CGContextAddLineToPoint(context, 15.0, 30.0);
    CGContextAddLineToPoint(context, 0.0, 15.0);
    CGContextAddLineToPoint(context, -15.0, 30.0);
    CGContextAddLineToPoint(context, 0.0, -15.0);
    //Return lines to the path
    CGContextFillPath(context);
    CGContextRestoreGState(context);
}

#pragma mark - Accessors

- (void)moveShip:(float)distance angle:(float)angle {
    shipLocation.x -= (distance * 3.0) * cos((angle + 90.0) * 0.0174532925);
    shipLocation.y -= (distance * 3.0) * sin((angle + 90.0) * 0.0174532925);
    shipDirection = angle;
    
    [self setNeedsDisplay];
}

#pragma mark - Memory management

- (void)dealloc {
    //[super dealloc]; //Not possible anymore in IOS5 during NDA
}
@end
